package PowerWord.UI.StudyMode;

import PowerWord.Core.Plan.Controller;
import PowerWord.UI.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class StudyModePreference extends PreferenceActivity
								 implements OnSharedPreferenceChangeListener{
	
	public static final String PrefKey_WordList = "WordList";
	
	public static final String PrefKey_FreeMode = "FreeMode";
	public static final String PrefKey_StartWord = "StartWord";
	public static final String PrefKey_EndWord = "EndWord";
	
	public static final String PrefKey_PlanMode = "PlanMode";
	public static final String PrefKey_DayCount = "DayCount";
	public static final String PrefKey_TodayDuty = "TodayDuty";
	
	private CheckBoxPreference mFreeMode = null;
	private CheckBoxPreference mPlanMode = null;
	
    public static void UpdateStudyModeSettings(Context context){
    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    	Controller.Instance().ChangeWordList(prefs.getString(PrefKey_WordList, ""));
    	if(prefs.getBoolean(PrefKey_FreeMode, false)){
    		Controller.Instance().SwitchStudyMode(true);
    		Controller.Instance().InitDuty(prefs.getString(PrefKey_StartWord, ""),
    									   prefs.getString(PrefKey_EndWord, ""), 0);
    	}
    	else{
    		Controller.Instance().SwitchStudyMode(false);
    		Controller.Instance().InitDuty(prefs.getString(PrefKey_DayCount, "200"),
					 					   prefs.getString(PrefKey_TodayDuty, "0"), 0);
    	}
    }
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.study_plan_setting);
        mFreeMode = (CheckBoxPreference)getPreferenceScreen().findPreference(PrefKey_FreeMode);
        mPlanMode = (CheckBoxPreference)getPreferenceScreen().findPreference(PrefKey_PlanMode);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }
    
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        // Let's do something when my counter preference value changes
        if (key.equals(PrefKey_FreeMode)) {
        	mPlanMode.setChecked(!sharedPreferences.getBoolean(key, false));
        }
        if (key.equals(PrefKey_PlanMode)) {
        	mFreeMode.setChecked(!sharedPreferences.getBoolean(key, false));
        }
    }
}
